<?php

namespace Admin\Controller;

class PhantomController extends AdminController{
	
	function infolist(){
		$nickname       =   I('nickname');
		$map['phone']    =   array('like', '%'.(string)$nickname.'%');
		$list   = $this->lists('Userinfo', $map);
		int_to_string($list);
		$this->assign('_list', $list);
		$this->meta_title = '用户信息';
		$this->display();
	}
	
	
	function userlist(){
		$nickname       =   I('nickname');
	    $map['phone']    =   array('like', '%'.(string)$nickname.'%');
		$list   = $this->lists('User', $map);
		int_to_string($list);
		$this->assign('_list', $list);
		$this->meta_title = '用户信息';
		$this->display();
	}
	
	function saveuserinfo(){
		
		$id = intval(I('id'));
		$model = D('Userinfo');
		if(IS_POST){
			$phone = I('phone');
			$photo = I('photo');
			$age = I('age');
			$nationality = I('nationality');
			$city = I('city');
			if(empty($photo)){
				$this->error('请上传照片');
				exit;
			}
			if(!preg_match("/^1[3456789]\d{9}$/", $phone)){
				$this->error('手机号码错误');
				exit;
			}
			$info = [
		    	'phone'=>$phone,
		    	'photo'=>$photo,
		    	'age' => intval($age),
		    	'nationality'=>$nationality,
		    	'city'=>$city,
		    	'uname'=>I('uname'),
			];
			
			$res = 0;
			
			if($id){
				$res = $model->where(['id'=>$id])->save($info);
			}else{
				$res = $model->add($info);
			}
		
		if($res){
			$this->success($id?'更新成功':'添加成功',U('infolist'));
			exit;
		}
		$this->error('添加失败,可能手机号码已添加');
		}ELSE{
			$this->assign('info',$model->where(['id'=>$id])->find());
			
		}
		$this->display('add');
	}
	
	function achievement(){
		$phone = I('id');
		$model = D('AchievementInfo');
		$basemodel =  D('AchievementBase');
		if(IS_POST){
			$subjectKey=['phlss','phlss2','idpa','idpa2','rifle','rifle2','firstaid','firstaid2'];
			$data = $_POST;
			$save = [];
			foreach ($data as $key=>$val){
				$sb_index =1;
				foreach ($data[$key] as $info){
					$save[$key]['stage_'.$sb_index] = ($info['m']?:0).'\''.($info['s']?:0);
					$sb_index++;
				}
			}
			$phone = I('phone');
		
			$basemodel->where(['phone'=>$phone])->delete();
		
			$base_data = [
	    	   'phone'=>$phone,
	    	   'title_list'=>implode(',', I('title_list')),
	    	   'cert_list'=>implode(',', I('cret_list')),
	    	   'skill_list'=>implode(',', I('skill_list')),
	    	   'club'=>I('club')
			   
			];
			$basemodel->add($base_data);
			$base_id = $basemodel->getLastInsID();
			$model->where(['phone'=>$phone])->delete();
			foreach ($subjectKey as $key){
				$data = [
						'base_id'=>$base_id,
						'subject'=>$key,
						'stage_1'=>	$save[$key]['stage_1'],
					    'stage_2'=>	$save[$key]['stage_2'],
				        'stage_3'=>	$save[$key]['stage_3'] 
				];
				
				$model->add($data);
			}
		
			$this->success('录入成功');
		}else{
			if($phone){
				
				$base_info =$basemodel->where(['phone'=>$phone])->find();
				if($base_info){
					$info = $model->where(['base_id'=>$base_info['id']])->select();
					$this->fmtBaseInfo($base_info);
					$this->fmtAchievementInfoByEdit($info);
				    $this->assign('base_info',$base_info);
				    $this->assign('info',$info);
				}
			}
			$this->assign('phone',$phone);
		    $this->display();
		}
	}
	
	private function fmtAchievementInfoByEdit(&$data){
		foreach ($data as $key=>$value){
			$data[$value['subject']]['stage_1'] =  explode('\'', $data[$key]['stage_1']);
			$data[$value['subject']]['stage_2'] =  explode('\'', $data[$key]['stage_2']);
			$data[$value['subject']]['stage_3'] =  explode('\'', $data[$key]['stage_3']);
	
		}
	}
	
	
	private function fmtBaseInfo(&$data){
		$data['title_list'] = explode(',', $data['title_list']);
		
		$data['cert_list'] = explode(',', $data['cert_list']);
		$data['skill_list'] = explode(',', $data['skill_list']);
	}
	
	private function fmtAchievementInfo(&$data){
		foreach ($data as $key=>$value){
		$data[$key]['stage_1'] = explode('\'', $data[$key]['stage_1']);
		$data[$key]['stage_2'] = explode("\'", $data[$key]['stage_2']);
		$data[$key]['stage_3'] = explode("\'", $data[$key]['stage_3']);
		}
	}
}

